package www.woniuxy.com.class112.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import www.woniuxy.com.class112.dao.TechnologyTypeMapper;
import www.woniuxy.com.class112.dao.UserIdentityMapper;
import www.woniuxy.com.class112.dao.UserInfosMapper;
import www.woniuxy.com.class112.dto.UserStandingDto;

import www.woniuxy.com.class112.entity.TechnologyType;
import www.woniuxy.com.class112.entity.UserIdentity;
import www.woniuxy.com.class112.entity.UserInfos;
import www.woniuxy.com.class112.param.userStanding.UserStandingFdAllParam;
import www.woniuxy.com.class112.param.userStanding.UserStandingrejectParam;
import www.woniuxy.com.class112.service.UserStandingService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserStandingServiceImpl implements UserStandingService {
    @Resource
    private UserIdentityMapper userIdentityMapper;
    @Resource
    private TechnologyTypeMapper technologyTypeMapper;
    @Resource
    private UserInfosMapper userInfosMapper;
    @Override
    public PageInfo<UserStandingDto> selectUserStanding(UserStandingFdAllParam param) throws Exception {
        PageHelper.startPage(param.getPageIndex() == null ? 1 : param.getPageIndex(), param.getPageSize() == null ? 5 : param.getPageSize());
        List<UserStandingDto> UserStandingDto =userIdentityMapper.selectUserStanding(param.getIdentityType(), param.getName(), param.getTypeId());
        return new PageInfo<>(UserStandingDto);
    }

    @Override
    public List<TechnologyType> selectType() {
        return technologyTypeMapper.selectByExample(null);
    }

    @Override
    public void passUserStanding(Long id, Long userId) {
        UserIdentity userIdentity = userIdentityMapper.selectByPrimaryKey(id.intValue());
        if (userIdentity==null){
            throw new RuntimeException("该申请不存在");
        }
        if (userIdentity.getIdentityType()==2){
            throw new RuntimeException("该申请已通过");
        }
        UserInfos userInfos = userInfosMapper.selectByPrimaryKey(userId);
        if (userInfos==null){
            throw new RuntimeException("该用户不存在");
        }
//        if (userInfos.getStateId()!=0){
//            throw new RuntimeException("该用户状态异常");
//        }
        userInfos.setTechnologyId(userIdentity.getTypeId());
        userInfosMapper.updateByPrimaryKey(userInfos);
        userIdentity.setIdentityType(2L);
        userIdentity.setAdminId(userId);
        userIdentity.setAuditReason(null);
        userIdentity.setAuditTime(new Date());
        userIdentityMapper.updateByPrimaryKey(userIdentity);
    }

    @Override
    public void rejectUserStanding(UserStandingrejectParam param) {
        UserIdentity userIdentity = userIdentityMapper.selectByPrimaryKey(param.getId().intValue());
        if (userIdentity==null){
            throw new RuntimeException("该申请不存在");
        }
        if (userIdentity.getIdentityType()==1){
            throw new RuntimeException("该申请驳回");
        }
        UserInfos userInfos = userInfosMapper.selectByPrimaryKey(userIdentity.getUserId());//通过身份认证的用户id查出用户
        if (userInfos==null){
            throw new RuntimeException("该用户不存在");
        }
//        if (userInfos.getStateId()!=0){
//            throw new RuntimeException("该用户状态异常");
//        }
        userInfos.setTechnologyId(1L);
        userInfosMapper.updateByPrimaryKey(userInfos);

        userIdentity.setIdentityType(1L);
        userIdentity.setAdminId(param.getUserId());
        userIdentity.setAuditTime(new Date());
        userIdentity.setAuditReason(param.getRejectReason());
        userIdentityMapper.updateByPrimaryKeySelective(userIdentity);
    }
}
